﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.ComponentModel;
using libConn;

namespace ApartmentSale
{
    public class InvoiceView : INotifyPropertyChanged, ICloneable
    {
        private int _invoice_id;
        public int invoice_id
        {
            get
            {
                return _invoice_id;
            }
            set
            {
                _invoice_id = value;
                NotifyPropertyChanged("invoice_id");
            }
        }

        private string _invoice_code;
        public string invoice_code
        {
            get
            {
                return _invoice_code;
            }
            set
            {
                _invoice_code = value;
                NotifyPropertyChanged("invoice_code");
            }
        }

        private int _invoice_status;
        public int invoice_status
        {
            get
            {
                return _invoice_status;
            }
            set
            {
                _invoice_status = value;
            }
        }

        private double _invoice_amount;
        public double invoice_amount
        {
            get
            {
                return _invoice_amount;
            }
            set
            {
                _invoice_amount = value;
                NotifyPropertyChanged("invoice_amount");
            }
        }

        private double _invoice_cash_amount;
        public double invoice_cash_amount
        {
            get
            {
                return _invoice_cash_amount;
            }
            set
            {
                _invoice_cash_amount = value;
                NotifyPropertyChanged("invoice_cash_amount");
            }
        }

        private double _invoice_card_amount;
        public double invoice_card_amount
        {
            get
            {
                return _invoice_card_amount;
            }
            set
            {
                _invoice_card_amount = value;
                NotifyPropertyChanged("invoice_card_amount");
            }
        }

        private double _invoice_check_amount;
        public double invoice_check_amount
        {
            get
            {
                return _invoice_check_amount;
            }
            set
            {
                _invoice_check_amount = value;
                NotifyPropertyChanged("invoice_check_amount");
            }
        }

        private string _invoice_custom_name;
        public string invoice_custom_name
        {
            get
            {
                return _invoice_custom_name;
            }
            set
            {
                _invoice_custom_name = value;
                NotifyPropertyChanged("invoice_custom_name");
            }
        }

        private DateTime _invoice_date;
        public DateTime invoice_date
        {
            get
            {
                return _invoice_date;
            }
            set
            {
                _invoice_date = value;
                NotifyPropertyChanged("invoice_date");
            }
        }

        private int _apartment_id;
        public int apartment_id
        {
            get
            {
                return _apartment_id;
            }
            set
            {
                _apartment_id = value;
                NotifyPropertyChanged("apartment_id");
            }
        }

        public string status_string
        {
            get
            {
                switch (invoice_status)
                {
                    case 0:
                        return "Valid";
                    case 1:
                        return "Invalid";
                }
                return "Valid";
            }
            set
            {
                switch (value)
                {
                    case "Valid":
                        invoice_status = 0;
                        break;
                    case "Invalid":
                        invoice_status = 1;
                        break;
                    default:
                        invoice_status = 0;
                        break;
                }
            }
        }

        public string date_string
        {
            get
            {
                return invoice_date.ToShortDateString();
            }
        }

        public bool has_changed { get; set; }

        public string apartment_code { get; set; }

        public InvoiceView(DataRow r)
        {
            invoice_id = Convert.ToInt32(r["invoice_id"]);
            invoice_code = Convert.ToString(r["invoice_code"]).Replace("''","'");
            invoice_status = Convert.ToInt32(r["invoice_status"]);
            invoice_amount = Convert.ToDouble(r["invoice_amount"]);
            invoice_cash_amount = Convert.ToDouble(r["invoice_cash_amount"]);
            invoice_card_amount = Convert.ToDouble(r["invoice_card_amount"]);
            invoice_check_amount = Convert.ToDouble(r["invoice_check_amount"]);
            invoice_custom_name = Convert.ToString(r["invoice_custom_name"]).Replace("''", "'");
            invoice_date = Convert.ToDateTime(r["invoice_date"]);
            apartment_id = Convert.ToInt32(r["apartment_id"]);

            Datarow = r;
            has_changed = false;
        }

        public InvoiceView()
        {
            invoice_id = 0;
            invoice_code = "";
            invoice_status = 0;
            invoice_amount = 0;
            invoice_cash_amount = 0;
            invoice_card_amount = 0;
            invoice_check_amount = 0;
            invoice_custom_name = "";
            invoice_date = DateTime.Now;
            apartment_id = 0;

            initDataRow();
            has_changed = false;
        }

        private DataRow _datarow;
        public DataRow Datarow
        {
            set
            {
                _datarow = value;
            }
            get
            {
                refreshDataRow();
                return _datarow;
            }
        }

        public string queryString
        {
            get
            {
                return "SELECT * FROM invoice WHERE invoice_id = " + invoice_id;
            }
        }
        public string deleteString
        {
            get
            {
                return "DELETE FROM invoice WHERE invoice_id = " + invoice_id;
            }
        }

        public readonly string tableName = "invoice";
        public readonly int ColumnCount = 10;

        private void initDataRow()
        {
            string errMessage = "";
            DataTable table = AccdbConn.query(queryString, ref errMessage);
            Datarow = table.NewRow();
        }

        private void refreshDataRow()
        {
            DataRow r = _datarow;
            r["invoice_id"] = invoice_id;
            r["invoice_code"] = invoice_code.Replace("'", "''");
            r["invoice_status"] = invoice_status;
            r["invoice_amount"] = invoice_amount;
            r["invoice_cash_amount"] = invoice_cash_amount;
            r["invoice_card_amount"] = invoice_card_amount;
            r["invoice_check_amount"] = invoice_check_amount;
            r["invoice_custom_name"] = invoice_custom_name.Replace("'", "''");
            r["invoice_date"] = invoice_date;
            r["apartment_id"] = apartment_id;
        }

        public string Insert()
        {
            string errMessage = "";

            if (!AccdbConn.insert(queryString, tableName, ColumnCount, Datarow, ref errMessage))
            {
                errMessage = "Add invoice failed.\n" + errMessage;
                has_changed = true;
            }
            else
            {
                has_changed = false;
            }

            return errMessage;
        }

        public string Update()
        {
            string errMessage = "";

            if (!AccdbConn.update(queryString, tableName, ColumnCount, Datarow, ref errMessage))
            {
                errMessage = "Failed to modify the invoice.\n" + errMessage;
                has_changed = true;
            }
            else
            {
                has_changed = false;
            }

            return errMessage;
        }

        public string Delete()
        {
            string errMessage = "";
            AccdbConn.exec(deleteString, ref errMessage);

            if (errMessage != "")
                errMessage = "Failed to delete the invoice.\n" + errMessage;

            return errMessage;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                has_changed = true;
            }
        }

        public object Clone()
        {
            return new InvoiceView(Datarow);
        }

    }
}
